package com.example.algorithm.mianshi;

import java.util.concurrent.Semaphore;

/**
 * @author: dangwen
 * @createDate: 2021/6/30
 */
public class PrintABC02 {
    static Semaphore a = new Semaphore(1);
    static Semaphore b = new Semaphore(0);
    static Semaphore c = new Semaphore(0);

    static class A implements Runnable{
        @Override
        public void run() {
            try {
                a.acquire();
                System.out.println("A");
                b.release();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static class B implements Runnable{
        @Override
        public void run() {
            try {
                b.acquire();
                System.out.println("B");
                c.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static class C implements Runnable{
        @Override
        public void run() {
            try {
                c.acquire();
                System.out.println("C");
                a.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread t1 = new Thread(new A());
            Thread t2 = new Thread(new B());
            Thread t3 = new Thread(new C());
            t1.start();
            t2.start();
            t3.start();
        }
    }

}
